package com.yuandian.business.monitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.po.UserActionDetailParams;
import com.yuandian.api.business_monitor.vo.UserActionBusiStepListVo;
import com.yuandian.api.business_monitor.vo.UserActionDetailVO;
import com.yuandian.api.management.feign.RemoteExclusiveIndicatorService;
import com.yuandian.business.monitor.mapper.UserActionDetailServiceMapper;
import com.yuandian.business.monitor.service.IUserActionDetailService;
import com.yuandian.utils.BeanUtils;
import com.yuandian.utils.PageDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author liXin
 * @description 用户操作详情
 * @date 2021/12/6
 */
@Slf4j
@Service
public class UserActionDetailServiceImpl implements IUserActionDetailService {


    @Autowired
	UserActionDetailServiceMapper userActionDetailServiceMapper;
	@Autowired
	private RemoteExclusiveIndicatorService remoteExclusiveIndicatorService;
    /**
     * 获取用户操作详情
     *
     * @param params
     * @return
     */
    @Override
    public List<UserActionDetailVO> queryUserActionDetail(UserActionDetailParams params) {
        List<UserActionDetailVO> userActionDetailVOS = userActionDetailServiceMapper.queryUserActionDetail(params);
		if (userActionDetailVOS != null) {
			for (UserActionDetailVO userActionDetailVO : userActionDetailVOS) {
				String exclusiveIndicator = userActionDetailVO.getExclusiveIndicator();
				Map<String, List<String>> businessIndicatorsShowMap = remoteExclusiveIndicatorService.getBusinessIndicatorsShowMap().getData();
				userActionDetailVO.setExclusiveIndicator(ExclusiveIndicator.getIndicatorWithSet(exclusiveIndicator, businessIndicatorsShowMap.get(userActionDetailVO.getBusinessCode())));
			}
		}
        return userActionDetailVOS;
    }

    /**
     * 用户操作业务步骤列表
     *
     * @param params
     * @return
     */
    @Override
    public PageDataInfo queryUserActionBusiStepList(UserActionDetailParams params) {

        ArrayList<UserActionBusiStepListVo> userActionDetailVOS = new ArrayList<>();
        Page<Map<String,Object>> page = new Page<>(params.getPageNum(), params.getPageSize());
        List<Map<String,Object>> list = userActionDetailServiceMapper.queryUserActionBusiStepList(page, params);
        //5.查询

        if (page.getTotal() > 0) {

            //6.组装数据
            for (Map<String, Object> sourceMap : list) {
                UserActionBusiStepListVo userActionBusiStepListVo = BeanUtils.mapToObject(sourceMap,
                        UserActionBusiStepListVo.class, Arrays.asList(new String[]{"id", "businessStepDataId", "startTime","endTime"}));
                userActionBusiStepListVo.setId(String.valueOf(sourceMap.get("id")));
                userActionBusiStepListVo.setBusinessStepDataId(String.valueOf(sourceMap.get("businessStepDataId")));
                userActionBusiStepListVo.setStartTime(((LocalDateTime)(sourceMap.get("startTime"))).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                userActionBusiStepListVo.setEndTime(((LocalDateTime)(sourceMap.get("endTime"))).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                //如果系统返回码是200或302 系统响应状态为成功,否则为失败
                if(Objects.equals(200,userActionBusiStepListVo.getServerResCode()) || Objects.equals(302,userActionBusiStepListVo.getServerResCode())){
                    userActionBusiStepListVo.setResponseStatus("成功");
                }else {
                    userActionBusiStepListVo.setResponseStatus("失败");
                }

                userActionDetailVOS.add(userActionBusiStepListVo);
            }
        }

        PageDataInfo pageDataInfo = new PageDataInfo(page.getTotal(), userActionDetailVOS);
        return pageDataInfo;
    }

}
